package org.meandslava.calc;

import org.meandslava.calc.arithmetic.Expression;
import org.meandslava.calc.arithmetic.FixedNumber;
import org.meandslava.calc.fragments.BaseButtonsFragment;
import org.meandslava.calc.fragments.SpecialButtonsFragment;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends SherlockFragmentActivity {
    private Expression rootExpression;
	private TextView display;
	//private TextView expression;
	private EditText expression;
	private SherlockFragment lastFragment;
    private static BaseButtonsFragment base = null;
    private static SherlockFragment additional = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        rootExpression = new Expression();
        rootExpression.setExpressionAsRoot(true);
        
        display = (TextView)findViewById(R.id.display);
        //expression = (TextView)findViewById(R.id.expression);
        expression = (EditText)findViewById(R.id.expression);
        expression.setInputType(InputType.TYPE_NULL);
                
        ImageButton ac = (ImageButton)findViewById(R.id.ac_button);
        ac.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				rootExpression = new Expression();
		        rootExpression.setExpressionAsRoot(true);
				resetDisplays();
			}
		});
        
        ImageButton back = (ImageButton)findViewById(R.id.back_button);
        back.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				rootExpression.back();
				resetDisplays();
			}
		});
        
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        
        if(base == null){
        	base = new BaseButtonsFragment();
        }
        ft.replace(R.id.contentplace, base);
        lastFragment = base; 
        ft.commit();
        fm.executePendingTransactions();
        
        resetDisplays();
    }
    
    public void onClick(View view){
    	String tag = (String)view.getTag();
    	//int actionCode = Integer.parseInt(tag);
		//rootExpression.addSymbol(actionCode);
		rootExpression.process(tag);
		
		resetDisplays();
    }
    
    public void onClickEqual(View view){
		FixedNumber res = new FixedNumber(rootExpression.getValue());

		rootExpression = new Expression();
        rootExpression.setExpressionAsRoot(true);
        rootExpression.addTerm(res);
		
		resetDisplays();
    }
    
    public void changeGroup(View view){
    	FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        
        SherlockFragment newFragment;
		if(getResources().getString(R.string.group_func).equals(view.getTag())){
			if(additional == null){
				additional = new SpecialButtonsFragment();
	        }
			newFragment = additional;
			Log.d("", "Changing fragment to additional");
		}else{
			if(base == null){
	        	base = new BaseButtonsFragment();
	        }
			newFragment = base;
			Log.d("", "Changing fragment to base");
		}
		if (!newFragment.equals(lastFragment)) {
			ft.replace(R.id.contentplace, newFragment);
			lastFragment = newFragment;
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

			ft.commit();
			fm.executePendingTransactions();
		}
    }
    
    private void resetDisplays(){
    	display.setText(rootExpression.toString());
		Editable e = expression.getEditableText();
		e.clear();
		rootExpression.write(e);
    }
}